package com.jd.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jd.util.CosBootUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.qcloudsms.SmsSingleSenderResult;
import com.jd.entity.RulesEntity;
import com.jd.service.RulesService;
import com.jd.util.Constants;
import com.jd.util.QRCodeUtil;
import com.jd.util.SmsServiceUtil;

@Controller
@RequestMapping("/uploadController")
public class UploadController {

	@Autowired
	private RulesService rulesService;
	
	@RequestMapping("/upload")
	@ResponseBody
	 public String upload(HttpServletRequest request) {
		  //String ctxPath = "D://upFiles";
		 String ctxPath = "/opt/jeecg-game/upload";
		  String fileName = null;
		  String bizPath = "files";
		  String pic="";
		  try {
		   String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
		   File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
		   if (!file.exists()) {
		    file.mkdirs();// 创建文件根目录
		    System.out.println(file+"3333333333333333333333333333");
		   }
		   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		   MultipartFile filePic = multipartRequest.getFile("filePic");// 获取上传文件对象
		    String orgName = filePic.getOriginalFilename();// 获取文件名
		    System.out.println(orgName+"11111111111111111111111111111111111");
		    //fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
		    fileName = System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
		    System.out.println(fileName+"22222222222222222222222222222222");
		    String savePath = file.getPath() + File.separator + fileName;
		    File savefile = new File(savePath);
		    FileCopyUtils.copy(filePic.getBytes(), savefile);
		    String dbpath = bizPath + File.separator + nowday + File.separator + fileName;
		    if (dbpath.contains("\\")) {
		     dbpath = dbpath.replace("\\", "/");
		    }
		    pic = dbpath;
		    System.out.println(pic+"222222222222222222222222222");
		   if(pic.endsWith(",")){
			   pic = pic.substring(0,pic.length()-1);
		   }
		   return pic;
		  } catch (IOException e) {
			   e.getMessage();
			  }
		return "";
	 }
    public BufferedImage loadImageLocal(String imgName) {
        try {
        	return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
  //生成海报
    @RequestMapping("/invitation")
	@ResponseBody
	public String makePoster(HttpServletRequest request) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String type = request.getParameter("type");
		String userId =   "555";//(String)request.getSession().getAttribute("userId");
		UploadController demo = new UploadController();
		//String po = "/opt/jeecg-game/upload/";
		String po = "C://upFiles/";
		System.out.println(po+"files/vote.jpg");
		RulesEntity rules = rulesService.getRules();
		String pic="";
		if(type.equals("1")){
			pic=rules.getExtensionPic();
		}else{
			pic=rules.getPlayerPic();
		}
		BufferedImage image = demo.loadImageUrl1(pic);
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        System.out.println(image+"imageimage00000000000000000000000000000");
        Graphics2D g = (Graphics2D)image.getGraphics();
        int w = image.getWidth();
        int h = image.getHeight();
        g = image.createGraphics();
        //����ͼƬ
        BufferedImage d;

        //����
        g.setFont(new Font("黑体", Font.BOLD, 20));
        g.setColor(Color.BLACK);

        //��ά��

        //������ά��ͼƬ
        Long cur = System.currentTimeMillis();
        String picName = cur + ".jpg";
        //String serverAddr = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+File.separator+"BV2"+File.separator+"QrCode";
        //
        try {
        	
        	QRCodeUtil.encode("http://www.bfwcg.com/game/wechat/authorize?id="+userId+"&returnUrl=asdasdasd&type="+type,"", po, true, picName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedImage qr = demo.loadImageLocal(po+picName);
        System.out.println(qr+"qr+++++++++++++++++++++++++++++++++");
        System.out.println(picName+"qr+++++++++++++++++++++++++++++++++");

        if(type.equals("1")){
       	 g.drawImage(qr,235,300,450,450,null);
       }else{
       	 g.drawImage(qr,265,330,380,380,null);
       }
        //����ͶƱ
        g.setFont(new Font("黑体", Font.BOLD, 40));
        g.dispose();
        File outputfile=null;
        if(type.equals("1")){
        	outputfile = new File(po+userId+"1.jpg");
        }else{
    	   outputfile = new File(po+userId+"2.jpg");
        }
        try {
            ImageIO.write(image, "jpg", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
		if(null != outputfile){
			return CosBootUtil.upload(outputfile, "qr");
		}
		return null;
	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public BufferedImage loadImageUrl1(String imgName) {
        try {
            URL url = new URL(imgName);
            BufferedImage _img = javax.imageio.ImageIO.read(url);
            BufferedImage _newImg= new BufferedImage(_img.getWidth(),_img.getHeight(),BufferedImage.TYPE_INT_RGB);
            _newImg.getGraphics().drawImage(_img, 0, 0, null);
            return _newImg;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
}
